package com.bkhc.partner.service;

import com.bkhc.common.permissions.bean.UserPermission;
import com.bkhc.common.service.UserService;
import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultCode;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.OrderItemVo;
import com.bkhc.common.vo.OrderVo;
import com.bkhc.common.vo.UserBean;
import com.bkhc.partner.entity.Employee;
import com.bkhc.partner.entity.PartnerBrand;
import com.bkhc.partner.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderService {
    @Autowired
    private PartnerRepository partnerRepository;

    @Autowired
    private EmployeeRepository employeeRepository;

    @Autowired
    private PartnerBrandRepository partnerBrandRepository ;

    @Autowired
    private OrderRepository orderRepository ;

    @Autowired
    private ProductRepository productRepository ;

    @Autowired
    private UserService userService;

    public ResultJson<?> getOrders(OrderVo order, int pageNum, int pageSize) {
        UserBean crruentUser = CommonUtil.getCrruentUser();

        UserPermission userPermission = userService.queryUserPermission(crruentUser.getUserId());
        boolean isPatner = userPermission.getRoles().stream().anyMatch(roleDetail -> "partner".equals(roleDetail.getRole_code()));
        PageUtil res = null;
        if(isPatner){
            if(order == null){
                order = new OrderVo();
            }
            order.setOrderAuditStatus(1111);
            Employee employ = employeeRepository.findByUserId(crruentUser.getUserId());
            String partnerCode = employ.getPartnerCode();
            order.setProductCode(partnerCode);
            res = orderRepository.getPartnerCodeOrders(order,pageNum,pageSize);
            if(!CollectionUtils.isEmpty(res.getList())){
                List<Map<String,Object>> orders= (List<Map<String,Object>>) res.getList();
                List<String> allgoodsCode = new ArrayList<>();
                for(Map<String,Object> map:orders){
                    List<Map<String,Object>> orderitems= (List<Map<String,Object>>)map.get("orderitems");
                    for(Map<String,Object> orderitem:orderitems){
                        allgoodsCode.add(orderitem.get("goodsCode")+"");
                    }
                }
                List<Object> allspes = productRepository.findByGoodsCodeIn(allgoodsCode);
                for(Map<String,Object> map:orders){
                    List<Map<String,Object>> orderitems= (List<Map<String,Object>>)map.get("orderitems");
                    for(Map<String,Object> orderitem:orderitems){
                        orderitem.put("goodsSpecList",allspes.stream().filter(x->((Map)x).get("goodsCode").equals(orderitem.get("goodsCode")+"")));
                    }
                }
        }
}
        ResultJson<PageUtil> resultJson = ResultJson.success();
        resultJson.setData(res);
        return resultJson;
    }



    public ResultJson<?> complateOrder(String orderCode) {
        return updateOrderStatusByPartner(orderCode,11111+"");
    }

    public ResultJson<?> cancelOrder(String orderCode) {
        return updateOrderStatusByPartner(orderCode,11110+"");
    }

    public ResultJson<?>  updateOrderStatusByPartner(String orderCode,String orderStatus){

        OrderVo order = new OrderVo();
        order.setOrderCode(orderCode);
        order.setOrderAuditStatus(Integer.parseInt(orderStatus));
        UserBean crruentUser = CommonUtil.getCrruentUser();
        String userId=crruentUser.getUserId();
        UserPermission userPermission = userService.queryUserPermission(crruentUser.getUserId());
        boolean isPatner = userPermission.getRoles().stream().anyMatch(roleDetail -> "partner".equals(roleDetail.getRole_code()));
        if(isPatner){
            orderRepository.updateOrderAuditStatus(orderCode,Integer.parseInt(orderStatus));
        }
        ResultJson<PageUtil> resultJson = ResultJson.success();
        return resultJson;
    }
}
